package com.atom.app.lifecycle.base

import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.atom.app.lifecycle.bean.Account

@Database(entities = [Account.Bean::class], version = 1, exportSchema = false)
abstract class AppDBCommon : RoomDatabase() {

    abstract fun accountDao(): Account.DAO

    companion object {
        // Singleton prevents multiple instances of database opening at the
        // same time.
        @Volatile
        private var INSTANCE: AppDBCommon? = null

        fun getDatabase(): AppDBCommon {
            // if the INSTANCE is not null, then return it,
            // if it is, then create the database
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    AppApplication.getInstance(),
                    AppDBCommon::class.java,
                    AppConfig.commonDataBasePath.absolutePath
                ).build()
                INSTANCE = instance
                instance
            }
        }
    }
}
